/* eslint-disable camelcase */
import store from './store';
chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    details.requestHeaders.push({
      name: 'origin',
      value: 'https://juejin.cn',
    });
    return {
      requestHeaders: details.requestHeaders,
    };
  },
  {
    urls: ['*://*.juejin.cn/*'],
  },
  ['blocking', 'requestHeaders', 'extraHeaders']
);

// 抽奖
const drawFn = async () => {
  // 查询今日是否有免费抽奖机会
  const today = await fetch('https://api.juejin.cn/growth_api/v1/lottery_config/get', {
    headers: {
      cookie: document.cookie,
    },
    method: 'GET',
    credentials: 'include',
  }).then(res => res.json());

  if (today.err_no !== 0) return console.warn('免费抽奖失败！');
  if (today.data.free_count === 0) return console.log('今日已经免费抽奖！');

  // 免费抽奖
  const draw = await fetch('https://api.juejin.cn/growth_api/v1/lottery/draw', {
    headers: {
      cookie: document.cookie,
    },
    method: 'POST',
    credentials: 'include',
  }).then(res => res.json());

  if (draw.err_no !== 0) return console.warn('免费抽奖失败！');
  [3, 4].includes(draw.data.lottery_type) ? alert(`恭喜抽到：${draw.data.lottery_name}`) : console.log(`恭喜抽到：${draw.data.lottery_name}`);
};

// 签到
(async () => {
  /** 用户信息 */
  const userInfo = await fetch('https://api.juejin.cn/user_api/v1/user/get', {
    headers: {
      cookie: document.cookie,
    },
    method: 'GET',
    credentials: 'include',
  }).then(res => res.json());
  console.log(userInfo);
  store.dispatch('setFoo', userInfo);
  console.log(store.getters.foo);

  // 查询今日是否已经签到
  const today_status = await fetch('https://api.juejin.cn/growth_api/v1/get_today_status', {
    headers: {
      cookie: document.cookie,
    },
    method: 'GET',
    credentials: 'include',
  }).then(res => res.json());

  if (today_status.err_no !== 0) return console.warn('签到失败！');
  if (today_status.data) {
    console.log('今日已经签到！');
    drawFn();
    return;
  }

  const check_in = await fetch('https://api.juejin.cn/growth_api/v1/check_in', {
    headers: {
      cookie: document.cookie,
    },
    method: 'POST',
    credentials: 'include',
  }).then(res => res.json());

  console.log(check_in.err_no);
  console.log(check_in);

  if (check_in.err_no !== 0) console.warn('签到失败！');
  console.log(`签到成功！当前积分；${check_in.data.sum_point}`);
  drawFn();

  // 发送信息
  chrome.runtime.sendMessage(today_status, function(response) {
    console.log(response);
  });

  // 接收信息
  chrome.runtime.onMessage.addListener(message => {
    console.log(message);
  });
})();
alert(`Hello ${store.getters.foo}!!`);
